package com.zjdiepu.www.biz.system;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.system.BaseRemitAccountMapper;
import com.zjdiepu.www.dao.system.BaseSalersmanMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.system.IBaseRemitAccountService;
import com.zjdiepu.www.model.system.dto.BaseRemitAccount;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.model.system.ext.BaseRemitAccountExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 打款名单维护Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年05月18日 18时23分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseRemitAccountServiceImpl extends BaseService implements IBaseRemitAccountService {
	public static final Logger logger = LoggerFactory.getLogger(BaseRemitAccountServiceImpl.class);

	@Autowired
	private BaseRemitAccountMapper baseRemitAccountMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private BaseSalersmanMapper baseSalersmanMapper;
	
	@Override
	public BaseRemitAccount queryByBusiId(String busiId, String fundChannel) {
		if(StringUtils.isBlank(busiId)) {
			ExceptionHelper.thow("999", "业务ID缺失");
		}
		YwBusiness ywBusiness = ywBusinessMapper.selectByPrimaryKey(busiId);
		if(null == ywBusiness) {
			ExceptionHelper.thow("999", "业务记录缺失或已被删除");
		}
		return queryBySalesmanId(ywBusiness.getSalesmanId(), fundChannel);
	}
	
	@Override
	public BaseRemitAccount queryBySalesmanId(String salesmanId,
			String fundChannel) {
		if(StringUtils.isBlank(salesmanId)) {
			ExceptionHelper.thow("999", "业务员ID缺失");
		}
		BaseSalersman baseSalersman = baseSalersmanMapper.selectByPrimaryKey(salesmanId);
		if(null == baseSalersman) {
			ExceptionHelper.thow("999", "业务员记录缺失或已被删除");
		}
		String unitCode = baseSalersman.getUnitCode();
		return queryByOrgCode(unitCode, fundChannel);
	}
	
	@Override
	public BaseRemitAccount queryByOrgCode(String orgCode, String fundChannel) {
		if(StringUtils.isBlank(orgCode)) {
			ExceptionHelper.thow("999", "业务单位编码缺失");
		}
		if(StringUtils.isBlank(fundChannel)) {
			ExceptionHelper.thow("999", "打款账号资金渠道缺失");
		}
		BaseRemitAccount paramRemitAccount = new BaseRemitAccount();
		paramRemitAccount.setDpStatus(DpStatus.NORMAL.getCode());
		paramRemitAccount.setFundChannel(fundChannel);
		paramRemitAccount.setOrgCode(orgCode);
		BaseRemitAccount baseRemitAccount = baseRemitAccountMapper.selectRefined(paramRemitAccount);
		if(null == baseRemitAccount) {
			int codeLen = orgCode.length();
			if(codeLen == 2) {
				return null;
			} else if(codeLen == 4) {
				orgCode = orgCode.substring(0, codeLen - 2);
				return queryByOrgCode(orgCode, fundChannel);
			} else if(codeLen >= 7){
				orgCode = orgCode.substring(0, codeLen - 3);
				return queryByOrgCode(orgCode, fundChannel);
			} else {
				ExceptionHelper.thow("999", "业务机构编码格式不对");
				return null;
			}
		} else {
			return baseRemitAccount;
		}
	}
	
	@Override
	public void saveUpdateAndDelete(BaseRemitAccount baseRemitAccount, OperateType operType) {
		switch (operType) {
		case add:
			baseRemitAccount.initInsertData(baseRemitAccount.getModifier(), baseRemitAccount.getModifierName());
			baseRemitAccountMapper.insert(baseRemitAccount);
			break;
		case edit:
			baseRemitAccount.initUpdateData(baseRemitAccount.getModifier(), baseRemitAccount.getModifierName());
			// 影响的记录数
			int updateCount = baseRemitAccountMapper.updateByPrimaryKeySelective(baseRemitAccount);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseRemitAccount.initDeleteData(baseRemitAccount.getModifier(), baseRemitAccount.getModifierName());
			// 影响的记录数
			int deleteCount = baseRemitAccountMapper.updateByPrimaryKeySelective(baseRemitAccount);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<BaseRemitAccount> selectByExt(BaseRemitAccountExt baseRemitAccountExt) {
		return baseRemitAccountMapper.selectByExt(baseRemitAccountExt);
	}
	
	@Override
	public int countByExt(BaseRemitAccountExt baseRemitAccountExt){
		return baseRemitAccountMapper.countByExt(baseRemitAccountExt);
	}
	
	@Override
	public BaseRemitAccount selectById(String id) {
		return baseRemitAccountMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseRemitAccountMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseRemitAccount baseRemitAccount) {
		return baseRemitAccountMapper.countRefined(baseRemitAccount);
	}

	@Override
	public BaseRemitAccount selectRefined(BaseRemitAccount baseRemitAccount) {
		return baseRemitAccountMapper.selectRefined(baseRemitAccount);
	}
	
	@Override
	public int deletedRefined(BaseRemitAccount baseRemitAccount) {
		return baseRemitAccountMapper.deletedRefined(baseRemitAccount);
	}

	@Override
	public boolean repeat(BaseRemitAccount baseRemitAccount) {
		int count = countRefined(baseRemitAccount);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseRemitAccount.getId())) {
			return true;
		}
		BaseRemitAccount dbItem = selectRefined(baseRemitAccount);
		if(null == dbItem) {
			return false;
		}
		return !baseRemitAccount.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return baseRemitAccountMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<BaseRemitAccount> baseRemitAccounts) {
		return baseRemitAccountMapper.insertBatch(baseRemitAccounts);
	}

	@Override
	public int countMulitByExt(BaseRemitAccountExt baseRemitAccountExt) {
		return baseRemitAccountMapper.countMulitByExt(baseRemitAccountExt);
	}

	@Override
	public List<BaseRemitAccount> selectMulitByExt(
			BaseRemitAccountExt baseRemitAccountExt) {
		return baseRemitAccountMapper.selectMulitByExt(baseRemitAccountExt);
	}
}
